//index.js
const app = getApp()
const db = wx.cloud.database();
// 在页面中定义插屏广告
let interstitialAd = null
Page({
  data: {
    nickName:'',
    avatarUrl:'',
    isLogin:0,
    count:0,
    level:0
    },

  onLoad: function (options) {

    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-86b7ee462f4f8bf7'
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  goRole(){
    wx.navigateTo({
      url: '../role/role',
    })
  },
  goCase(){
    wx.navigateTo({
      url: '../case/case',
    })
  },
  onShow:function(){
    var that=this
    wx.getStorage({
      key: 'user',
      success: function(res) {

        that.setData({
          isLogin: 1,
          nickName:res.data.nickName,
          avatarUrl:res.data.avatarUrl
        })
        that.getList()
      }, fail () {
      
      }
    })
  },

  getList:function(){
    var that=this
    //1、查询状态为是否存在打卡记录
    wx.cloud.callFunction({
      name: 'getopenid',//调用云函数获取用户唯一openid
      complete: res => {
        const openid = res.result.openid
        db.collection('ranking').where({
          _openid: openid,
        })
        .get({
          success: function(res) {
            if(res.data.length>0){
             
              var count=res.data[0].count
              var level=0
              if(count<=21){
                level=0
              }
              if(count>21&&count<=90){
                level=1
              }
              if(count>90&&count<=270){
                level=2
              }
              if(count>270&&count<=540){
                level=3
              }
              if(count>540&&count<=900){
                level=4
              }
              if(count>900&&count<=1350){
                level=5
              }
              if(count>1350&&count<=1890){
                level=6
              }
              if(count>1890){
                level=7
              }
              that.setData({
                count:count,
                level:level
              })
           
            }
          }
        })
      }
    })
  },
  /**
   * 进入打卡页面
   */
  onCheckAuth: function (e) {
    const db = wx.cloud.database()
    var that=this
    //一天只能提肛三次，超过三次，则无法提肛
    var date = new Date();
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    var DD = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) ;
    wx.cloud.callFunction({
      name: 'getopenid',//调用云函数获取用户唯一openid
      complete: res => {
        const openid = res.result.openid
        db.collection('record').where({
          _openid: openid,
          date:Y+M+DD
        }).count()
        .then(res=>{
          if(res.total>=3){
            wx.showToast({
              title: '一天撸管超过三次，请自行了断！',
              icon: 'none',
              duration: 3000
            })
          }else{
            wx.navigateTo({
              url: '../luadd/luadd',
            })
          }
        })
      }
    })
 
  },

  /**
   * 分享给好友
   */
  onShareAppMessage: function () {

 
  },



  //用户登录授权
  getUserProfile() {
    var that=this
   wx.getUserProfile({
     desc: '用于完善个人资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
     success: (res) => {
      console.log(res.userInfo)

      wx.setStorage({
        key: 'user',
        data: res.userInfo,
      })
      that.setData({
        isLogin: 1,
        nickName:res.userInfo.nickName,
        avatarUrl:res.userInfo.avatarUrl
      })
      wx.navigateTo({
        url: '../luadd/luadd',
      })
     }
   })
 },
})
